﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoomDataEmulator.Core
{
    public class TextilMachine: IMachine
    {
        #region Variables
        private int _RPM;
        private MachineStatus _status;
        private MachineRunningState _runningState;
        private IProductionOrder _currentOrder;
        private List<IProductionOrder> _productionOrder;
        #endregion

        #region Constructor
        public TextilMachine(int RPM)
        {
            if(RPM <= 0)
                throw new MachineRPMCannotBeZeroException();

            _RPM = RPM;
        }
        #endregion

        #region Properties
        public List<IProductionOrder> ProductionOrder
        {
            get
            {
                return _productionOrder;
            }
            set
            {
                _productionOrder = value;
            }
        }

        public IProductionOrder CurrentOrder
        {
            get
            {
                return _currentOrder;
            }
            set
            {
                _currentOrder = value;
            }
        }

        public MachineRunningState MachineRunningState
        {
            get
            {
                return _runningState;
            }
            set
            {
                _runningState = value;
            }
        }

        public MachineStatus MachineStatus
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
            }
        }

        public int RPM
        {
            get
            {
                return _RPM;
            }
            set
            {
                _RPM = value;
            }
        }

        #endregion

        #region Operations
        public void Delete(IProductionOrder order)
        {
            if (ProductionOrder != null)
            {
                ProductionOrder.Remove(order);
            }
        }

        public void Update(IProductionOrder order)
        {
            if (CurrentOrder != null)
            {
                CurrentOrder.RPMCompleted += this.RPM;
            }

            throw new NotImplementedException();
        }

        public void Insert(IProductionOrder order)
        {
            if (ProductionOrder != null)
            {
                if(order != null)
                    ProductionOrder.Add(order);
            }
        }

        public void Stop()
        {
            this.MachineStatus = MachineStatus.OFF;

            if(CurrentOrder != null)
                this.CurrentOrder.OrderStatus = ProductionOrderStatus.STOPPED;
        }

        public void Start()
        {
            this.MachineStatus = Core.MachineStatus.ON;

            if (CurrentOrder != null)
                this.CurrentOrder.OrderStatus = ProductionOrderStatus.RUNNING;
        }
        #endregion

    }
}
